LevelMenu.prototype = new Sprite();

function LevelMenu(){
    Sprite.apply(this);
    Menu.apply(this);
    var count = Game._game.levelOrder.length;
    var maxWidth = 0;
    this.buttons = new Array();
    for(var i = 0;i < count;i++){
        var levelName = Game._game.levelOrder[i];
        var buttonName;
        switch(levelName){
            case "levelCircuit":
                buttonName = "Obvod";
                break;
            case "levelElements":
                buttonName = "Prvky";
                break;
            case "levelFunctions":
                buttonName = "Funkcie";
                break;
            case "levelLogic":
                buttonName = "Logika";
                break;
            case "levelOptic":
                buttonName = "Optika";
                break;
            case "levelPi":
                buttonName = "Pí";
                break;
            case "levelPythagoras":
                buttonName = "Pythagoras";
                break;
            case "levelSort":
                buttonName = "Triedenie";
                break;
            case "levelTurtle":
                buttonName = "Korytnačka";
                break;
        }
        
        this.buttons.push (new Button(buttonName));
        var bHeight = this.buttons[i].height;
        var posun = i - (Math.floor(count/2));
        var posY = ((MainContainerHeight-bHeight)/2)+(bHeight+MARGIN)*posun;
        console.log("Pozicia " +posY);
        this.buttons[i].y = posY;
        this.buttons[i].id = i;
        maxWidth = Math.max(maxWidth, this.buttons[i].width);
        
        this.buttons[i].addEventListener(MouseEvent.CLICK, levelButtonClick);   
    }

    for(var i = 0;i < count;i++){
        this.buttons[i].setWidth(maxWidth);
        this.buttons[i].x = (MainContainerWidth-maxWidth)/2;
        this.addChild(this.buttons[i]);
    }
    
    this.backToMenu = new Button("Späť do menu");
    this.backToMenu.x = MARGIN;   
    this.backToMenu.y = MainContainerHeight - this.backToMenu.height - MARGIN;
    this.backToMenu.addEventListener(MouseEvent.CLICK, clickBack);
    this.addChild(this.backToMenu);
    
    
    function levelButtonClick (e){
        var level =Game._game.levels[Game._game.levelOrder[e.target.id]];
        Game._game.currentLevel = e.target.id*2;
        Game._game.StartLevel (level);
        
    }
    
    function clickBack (e){
        Game._game.LoadMainMenu();
        Game._game.selectLevelMode = false;
    }
}
